//
//  BaseViewController.m
//  ShanDian
//
//  Created by 静静 on 15/7/7.
//  Copyright (c) 2015年 诺万科技. All rights reserved.
//

#import "BaseViewController.h"
#import "NoNetWorkView.h"
#import "LoadFaildView.h"
#import "NoDataView.h"

@interface BaseViewController ()<NoNetWorkViewDelegate,LoadFaildViewDelegate>

@property (nonatomic,strong) NoNetWorkView  *noNetWorkView;
@property (nonatomic,strong) LoadFaildView  *loadFaildView;
@property (nonatomic,strong) NoDataView     *noDataView;

@end

@implementation BaseViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor colorWithHexString:baseViewBgColor alpha:1];
    self.viewtop = 0;
    self.viewbottom = 0;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onLoginNotiClicked:) name:NOTI_LOGIN object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NOTI_LOGIN object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    if (self.isViewLoaded && !self.view.window){
        self.view = nil;
    }
}

#pragma mark - private
/** 创建无网络页面 **/
- (NoNetWorkView *)noNetWorkView
{
    if (_noNetWorkView == nil) {
        _noNetWorkView = [NoNetWorkView createView];
        _noNetWorkView.frame = CGRectMake(0, self.viewtop, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - self.viewtop - self.viewbottom);
        _noNetWorkView.delegate = self;
    }
    return _noNetWorkView;
}

/** 创建加载失败页面 **/
- (LoadFaildView *)loadFaildView
{
    if (_loadFaildView == nil) {
        _loadFaildView = [LoadFaildView createView];
        _loadFaildView.frame = CGRectMake(0, self.viewtop, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - self.viewtop - self.viewbottom);
        _loadFaildView.delegate = self;
    }
    return _loadFaildView;
}

/** 创建列表无数据页面 **/
- (NoDataView *)noDataView
{
    if (_noDataView == nil) {
        _noDataView = [NoDataView createView];
        _noDataView.frame = CGRectMake(0, self.viewtop, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - self.viewtop - self.viewbottom);
    }
    return _noDataView;
}

/** 登录通知 **/
- (void)onLoginNotiClicked:(NSNotification *)notification
{
    
}
#pragma mark - public

/** 显示无网络页面 **/
- (void)showNoNetWorkView:(BOOL)show
{
    if (show) {
        [self.view addSubview:self.noNetWorkView];
    }else{
        [self.noNetWorkView removeFromSuperview];
    }
}
/** 显示加载失败页面 **/
- (void)showLoadFaildView:(BOOL)show
{
    if (show) {
        [self.view addSubview:self.loadFaildView];
    }else{
        [self.loadFaildView removeFromSuperview];
    }
}
/** 显示列表无数据页面 **/
- (void)showNoDataViewView:(BOOL)show
{
    if (show) {
        [self.noDataView viewWith:@"没有数据" with:@"不好！数据被外星人绑架了"];
        [self.view addSubview:self.noDataView];
    }else{
        [self.noDataView removeFromSuperview];
    }
}

/** 添加返回按钮back:YES **/
- (void)setBackBtn:(BOOL)back
{
    if (back == YES) {
        UIButton *backBtn = [UIView buttonWithFrame:CGRectMake(0, 0, 20, 32) ImageName:@"default_icon_return" BackImageName:nil Target:self Action:@selector(onBackBtnClicked) Title:nil];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    }else{
        UIButton *backBtn = [UIView buttonWithFrame:CGRectMake(0, 0, 0, 0) Title:nil TitleColor:[UIColor clearColor] TitleFont:14 Target:self Action:@selector(onBackBtnClicked)];
        backBtn.enabled = NO;
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    }
}

/** 设置Nav的Title **/
- (void)setNavTitle:(NSString *)title
{
    self.navigationItem.title = title;
}

/** 设置Nav左边的按钮 **/
- (void)setLeftNavBtn:(UIView *)leftBtn;
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
}

/** 设置Nav右边的按钮 **/
- (void)setRightNavBtn:(UIView *)rigntBtn;
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rigntBtn];
}

/** 设置Nav右边的一组按钮 **/
- (void)setRightNavBtns:(NSArray *)rigntBtns
{
    NSMutableArray *btnArray = [[NSMutableArray alloc] init];
    for (UIButton *btn in rigntBtns) {
        UIBarButtonItem *saoItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [btnArray addObject:saoItem];
    }
    self.navigationItem.rightBarButtonItems = btnArray;
}


#pragma mark - btn

/** 返回按钮方法重写 **/
- (void)onBackBtnClicked
{
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

#pragma mark - NoNetWorkViewDelegate
/** 网络异常刷新 **/
- (void)noNetWorkViewRefreshBtnClicked
{
    [self showNoDataViewView:NO];
    [self showNoNetWorkView:NO];
}

#pragma mark -LoadFaildViewDelegate
/** 加载失败 **/
- (void)loadFaildViewRefreshBtnClicked
{
    [self showNoDataViewView:NO];
    [self showLoadFaildView:NO];
}


@end
